package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.BorrowDao;
import dao.impl.BorrowDaoImpl;
import model.Borrow;
import model.User;

/**
 * Servlet implementation class BorrowServlet
 */
@WebServlet("/BorrowServlet")
public class BorrowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BorrowDao bwd;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BorrowServlet() {
		super();
		bwd = new BorrowDaoImpl();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = (String) request.getParameter("action");
		try {
			if (action.equals("borrowbook")) {
				borrowbook(request, response);
			} else if (action.equals("returnbook")) {
				returnbook(request, response);
			} else if(action.equals("agreebor")) {
				adminAgreeBorrow(request, response);
			} else if(action.equals("getBorList")) {
				getBorrowList(request, response);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


	protected void borrowbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String BookIsbn = request.getParameter("BookIsbn");
		String userid = request.getParameter("userid");
		String BorrowDuration = request.getParameter("BorrowDur");
		Borrow borrow = new Borrow();
		borrow.setBookIsbn(BookIsbn);
		borrow.setUserId(userid);
		borrow.setBorrowDuration(Integer.parseInt(BorrowDuration));
		HttpSession session = request.getSession();
		if (bwd.borrowBook(borrow)) {
			session.setAttribute("userborrow", bwd.getBorrowByUserId(userid));
			response.sendRedirect("pages/borrowlist.jsp");
		}else {
			response.sendRedirect("index.jsp");
		}
	}
	
	protected void returnbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String BookIsbn = request.getParameter("BookIsbn");
		String userid = request.getParameter("userid");
		Borrow borrow = new Borrow();
		borrow.setBookIsbn(BookIsbn);
		borrow.setUserId(userid);
		if (bwd.returnBook(borrow)) {
			HttpSession session = request.getSession();
			session.setAttribute("userborrow", bwd.getBorrowByUserId(userid));
			response.sendRedirect("pages/borrowlist.jsp");
		}else {
			response.sendRedirect("index.jsp");
		}
	}
	
	protected void adminAgreeBorrow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String BookIsbn = request.getParameter("BookIsbn");
		String userid = request.getParameter("userid");
		Borrow borrow = new Borrow();
		borrow.setBookIsbn(BookIsbn);
		borrow.setUserId(userid);
		if (bwd.adminAgreeBorrow(borrow)) {
			HttpSession session = request.getSession();
			session.setAttribute("userborrow", bwd.getAllBorrow());
			response.sendRedirect("pages/borrowlist.jsp");
		}else {
			response.sendRedirect("index.jsp");
		}
	}
	
	protected void getBorrowList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		HttpSession session = request.getSession();
		User logoner = (User) session.getAttribute("logoner");
		System.out.println();
		if(logoner.getUserGrade() == 0) {
			session.setAttribute("userborrow", bwd.getAllBorrow());
		}else {
			session.setAttribute("userborrow", bwd.getBorrowByUserId(logoner.getUserId()));
		}
		response.sendRedirect("pages/borrowlist.jsp");
	}

}
